class Solution {
public:
    vector<int> solve(vector<int>& a) {
        int n = a.size();
        vector<int> ret(n);
        int i = 0, aim = n;
        stack<int> s;
        bool hash[50007] = { false };
        for(auto& e : a)
        {
            s.push(e);
            hash[e] = true;
            while(hash[aim] == true) // 先更新目标值
            {
                --aim;
            }
            while(!s.empty() && s.top() >= aim) // 出栈
            {
                ret[i++] = s.top();
                s.pop();
            }
        }
        return ret;
    }
};